package com.miraclengine.karma;

import java.util.ArrayList;
import java.util.List;

import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.DelayModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.modifier.MoveModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.AnimatedSprite.IAnimationListener;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.sprite.ButtonSprite.OnClickListener;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.extension.texturepacker.opengl.texture.util.texturepacker.TexturePackTextureRegionLibrary;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.font.StrokeFont;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.util.HorizontalAlign;
import org.andengine.util.TimeUtils;
import org.andengine.util.modifier.IModifier;

import android.graphics.Typeface;

import com.badlogic.gdx.math.Vector2;
import com.miraclengine.eventobject.EventButtonSprite;
import com.miraclengine.eventobject.EventEntity;
import com.miraclengine.texture.TextureData;
import com.miraclengine.utils.Debug;



public class GameScene extends AbstractScene {
	
	private String TAG = GameScene.class.getSimpleName();

	private KarmaGame game;
	private TexturePackTextureRegionLibrary packer;
	
	//Font
	private Font mBitmapFont;
	private StrokeFont mAchieveFont;
	private Font mNameFont;
	
	public ITexture mTexture;
	
	private final static float ACHIEVE_FONT_SIZE = 20.f;
	private final static float NAME_FONT_SIZE = 18.f;
	
	public final static int LAYER_BG = 0;
	public final static int LAYER_FLOW = 1;
	public final static int LAYER_UI = 2;
	public final static int LAYER_SOCIAL_1 = 3;
	public final static int LAYER_SOCIAL_2 = 4;
	public final static int LAYER_COUNT = 5;
	
	//TextureRegion
	private TextureRegion bgRegion;
	
	private TextureRegion achieveBackRegion;
	private TextureRegion achieveBarRegion;
	private TextureRegion bookmarkBackRegion;
	private TextureRegion bookmarkIconRegion;
	
	private TextureRegion bookmarkEffect1Region;
	private TextureRegion bookmarkEffect2Region;
	private TiledTextureRegion bookmarkEffectRegion;
	private TextureRegion upRegion;
	private TextureRegion downRegion;
	
	private TextureRegion btnSocialNormalRegion;
	private TextureRegion btnSocialSelectRegion;
	
	private TextureRegion treeStartNormalRegion;
	private TextureRegion treeStartSelectRegion;
	
	private TextureRegion treeBeforeRegion;
	
	private TextureRegion treeAfter01Region;
	private TextureRegion treeAfter02Region;
	private TextureRegion treeAfter03Region;
	private TextureRegion treeAfter04Region;
	private TextureRegion treeAfter05Region;
	
	private TextureRegion treeAfter01SelectRegion;
	private TextureRegion treeAfter02SelectRegion;
	private TextureRegion treeAfter03SelectRegion;
	private TextureRegion treeAfter04SelectRegion;
	private TextureRegion treeAfter05SelectRegion;
	
	private TextureRegion bookmarkBoxRegion;
	private TextureRegion bookmarkMaxRegion;
	
	private TextureRegion socialFSelectRegion;
	private TextureRegion socialRNormalNRegion;
	private TextureRegion socialRNormalSRegion;
	private TextureRegion friendchoiceFNormalRegion;
	
	private TextureRegion btnBackNormalRegion;
	private TextureRegion btnBackSelectRegion;
	
	private TextureRegion socialRSelectRegion;
	private TextureRegion socialFNormalNRegion;
	private TextureRegion socialFNormalSRegion;
	private TextureRegion friendchoiceRNormalRegion;
	private TextureRegion friendchoiceRSelectRegion;
	
	private TextureRegion treeBeforeSelect01Region;
	private TextureRegion treeBeforeSelect02Region;
	
	private TextureRegion propic01Region;
	private TextureRegion propic02Region;
	private TextureRegion propic03Region;
	
	private TextureRegion socialRFromFRegion;
	private TextureRegion socialRToFRegion;
	
	private TextureRegion btnKakao01NormalRegion;
	private TextureRegion btnKakao02NormalRegion;
	private TextureRegion btnKakao03NormalRegion;
	private TextureRegion btnKakao04NormalRegion;
	
	private List<TextureRegion> propicRegion;
	
	private TextureRegion p_10Region;
	
	//Sprite
	private EventButtonSprite[] treeAfter = new EventButtonSprite[5];
	private EventButtonSprite treeStart;
	private Sprite achieveBar;
	private Sprite[] bookmarkIcon = new Sprite[5];
	private AnimatedSprite bookmarkEffect;
	private EventButtonSprite btnSocial;
	private Sprite bookmarkMax;
	private EventButtonSprite socialRNormalN;
	private EventButtonSprite btnBack1;
	private EventButtonSprite socialFNormalN;
	private EventButtonSprite btnBack2;
	
	//Text
	public Text mAchievePercentText;
	public Text mRemainTimeText;
	
	public GameScene(KarmaGame game) {
		// TODO Auto-generated constructor stub
		this.game = game;
		this.packer = game.mSpritesheetTexturePackTextureRegionLibrary;
		this.mTexture = game.spritesheetTexturePack.getTexture();
		
		loadResources();
	}
	
	private void loadResources() {
		//this.mBitmapFont = new BitmapFont(game.getTextureManager(), game.getAssets(), "font/numbers.fnt");
		//this.mBitmapFont.load();
		this.mBitmapFont = FontFactory.create(game.getFontManager(), game.getTextureManager(), 512, 512, Typeface.create(Typeface.DEFAULT, Typeface.NORMAL), 22, true, 0xff0000ff);
		this.mBitmapFont.load();
		
		final ITexture achieveFontTexture = new BitmapTextureAtlas(game.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
		this.mAchieveFont = new StrokeFont(game.getFontManager(), achieveFontTexture, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), ACHIEVE_FONT_SIZE, true, 0xffffffff, 2, 0xff804c11);
		this.mAchieveFont.load();
		
		this.mNameFont = FontFactory.create(game.getFontManager(), game.getTextureManager(), 256, 256, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), NAME_FONT_SIZE, true, 0xff804c11);
		this.mNameFont.load();
		
		//TextureRegion 
		this.bgRegion = this.packer.get(TextureData.BACK_ID);

		this.achieveBackRegion = this.packer.get(TextureData.ACHIEVE_BACK_ID);
		this.achieveBarRegion = this.packer.get(TextureData.ACHIEVE_BAR_ID);
		this.bookmarkBackRegion = this.packer.get(TextureData.BOOKMARK_BACK_ID);
		this.bookmarkIconRegion = this.packer.get(TextureData.BOOKMARK_ICON_ID);
		
		this.bookmarkEffect1Region = this.packer.get(TextureData.BOOKMARK_EFFECT01_ID);
		this.bookmarkEffect2Region = this.packer.get(TextureData.BOOKMARK_EFFECT02_ID);
		this.bookmarkEffectRegion = new TiledTextureRegion(this.mTexture, this.bookmarkEffect1Region, this.bookmarkEffect2Region);
		
		this.upRegion = this.packer.get(TextureData.UP_ID);
		this.downRegion = this.packer.get(TextureData.DOWN_ID);
		
		this.btnSocialNormalRegion = this.packer.get(TextureData.BTN_SOCIAL_NORMAL_ID);
		this.btnSocialSelectRegion = this.packer.get(TextureData.BTN_SOCIAL_SELECT_ID);
		
		this.treeStartNormalRegion = this.packer.get(TextureData.TREE_START_NORMAL_ID);
		this.treeStartSelectRegion = this.packer.get(TextureData.TREE_START_SELECT_ID);
		
		this.treeBeforeRegion = this.packer.get(TextureData.TREE_BEFORE_ID);
		
		this.treeAfter01Region = this.packer.get(TextureData.TREE_AFTER_01_ID);
		this.treeAfter02Region = this.packer.get(TextureData.TREE_AFTER_02_ID);
		this.treeAfter03Region = this.packer.get(TextureData.TREE_AFTER_03_ID);
		this.treeAfter04Region = this.packer.get(TextureData.TREE_AFTER_04_ID);
		this.treeAfter05Region = this.packer.get(TextureData.TREE_AFTER_05_ID);
		
		
		this.treeAfter01SelectRegion = this.packer.get(TextureData.TREE_AFTER_01_SELECT_ID);
		this.treeAfter02SelectRegion = this.packer.get(TextureData.TREE_AFTER_02_SELECT_ID);
		this.treeAfter03SelectRegion = this.packer.get(TextureData.TREE_AFTER_03_SELECT_ID);
		this.treeAfter04SelectRegion = this.packer.get(TextureData.TREE_AFTER_04_SELECT_ID);
		this.treeAfter05SelectRegion = this.packer.get(TextureData.TREE_AFTER_05_SELECT_ID);
		
		
		this.bookmarkBoxRegion = this.packer.get(TextureData.BOOKMARK_BOX_ID);
		this.bookmarkMaxRegion = this.packer.get(TextureData.BOOKMARK_MAX_ID);
		
		this.socialFSelectRegion = this.packer.get(TextureData.SOCIAL_F_SELECT_ID);
		this.socialRNormalNRegion = this.packer.get(TextureData.SOCIAL_R_NORMAL_N_ID);
		this.socialRNormalSRegion = this.packer.get(TextureData.SOCIAL_R_NORMAL_S_ID);
		this.friendchoiceFNormalRegion = this.packer.get(TextureData.FRIENDCHOICE_F_NORMAL_ID);
		this.btnBackNormalRegion = this.packer.get(TextureData.BTN_BACK_NORMAL_ID);
		this.btnBackSelectRegion = this.packer.get(TextureData.BTN_BACK_SELECT_ID);
		
		this.socialRSelectRegion = this.packer.get(TextureData.SOCIAL_R_SELECT_ID);
		this.socialFNormalNRegion = this.packer.get(TextureData.SOCIAL_F_NORMAL_N_ID);
		this.socialFNormalSRegion = this.packer.get(TextureData.SOCIAL_F_NORMAL_S_ID);
		this.friendchoiceRNormalRegion = this.packer.get(TextureData.FRIENDCHOICE_R_NORMAL_ID);
		this.friendchoiceRSelectRegion = this.packer.get(TextureData.FRIENDCHOICE_R_SELECT_ID);
		
		this.treeBeforeSelect01Region = this.packer.get(TextureData.TREE_BEFORE_SELECT01_ID);
		this.treeBeforeSelect02Region = this.packer.get(TextureData.TREE_BEFORE_SELECT02_ID);
		
		this.socialRFromFRegion = this.packer.get(TextureData.SOCIAL_R_FROMF_ID);
		this.socialRToFRegion = this.packer.get(TextureData.SOCIAL_R_TOF_ID);
		
		this.btnKakao01NormalRegion = packer.get(TextureData.BTN_KAKAO01_NORMAL_ID);
		this.btnKakao02NormalRegion = packer.get(TextureData.BTN_KAKAO02_NORMAL_ID);
		this.btnKakao03NormalRegion = packer.get(TextureData.BTN_KAKAO03_NORMAL_ID);
		this.btnKakao04NormalRegion = packer.get(TextureData.BTN_KAKAO04_NORMAL_ID);
		
		propicRegion = new ArrayList<TextureRegion>();
		
		this.propic01Region = packer.get(TextureData.PROPIC01_ID);
		propicRegion.add(this.propic01Region);
		this.propic02Region = packer.get(TextureData.PROPIC02_ID);
		propicRegion.add(this.propic02Region);
		this.propic03Region = packer.get(TextureData.PROPIC03_ID);
		propicRegion.add(this.propic03Region);
		
		this.p_10Region = packer.get(TextureData.P_10_ID);
	}
	
	EventButtonSprite treeBeforeSelect02;
	EventButtonSprite treeBeforeSelect01;
	
	@Override
	public void drawScene() {
		// TODO Auto-generated method stub
		
		this.setBackground(new Background(0, 0, 0));
		
		for(int i=0 ; i<LAYER_COUNT ; i++) {
			EventEntity entity = new EventEntity(this);
			entity.setTag(i);
			this.attachChild(entity);
		}
		
		this.getChildByIndex(LAYER_BG).attachChild(new Sprite(-143, 0, this.bgRegion, game.getVertexBufferObjectManager()));
		
		Sprite treeBefore = new Sprite(284, 221, this.treeBeforeRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LAYER_FLOW).attachChild(treeBefore);
		
		this.treeBeforeSelect01 = new EventButtonSprite(284, 221, this.treeBeforeSelect01Region,  this.treeBeforeSelect01Region, game.getVertexBufferObjectManager(), this, new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				game.switchGameToRunning(true, 0, 100, 0);
				if(Settings.soundEnabled) {
					game.mButtonSound.play();
				}
			}
		});
		this.registerTouchArea(this.treeBeforeSelect01);
		this.getChildByIndex(LAYER_FLOW).attachChild(this.treeBeforeSelect01);
		
		this.treeBeforeSelect02 = new EventButtonSprite(284, 404, this.treeBeforeSelect02Region, this.treeBeforeSelect02Region, game.getVertexBufferObjectManager(), this, new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				game.switchGameToRunning(true, 8, 100, 0);
				if(Settings.soundEnabled) {
					game.mButtonSound.play();
				}
			}
		});
		this.registerTouchArea(this.treeBeforeSelect02);
		this.getChildByIndex(LAYER_FLOW).attachChild(this.treeBeforeSelect02);
		
		//Tree Top
		this.treeAfter[0] = new EventButtonSprite(284, 221, this.treeAfter01Region, this.treeAfter01Region, game.getVertexBufferObjectManager(), this, new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				game.switchGameToRunning(true, 0, 100, 0);
				if(Settings.soundEnabled) {
					game.mButtonSound.play();
				}
			}
		});
		this.registerTouchArea(treeAfter[0]);
		this.getChildByIndex(LAYER_FLOW).attachChild(this.treeAfter[0]);
		
		//Start Button
		this.treeStart = new EventButtonSprite(332, 167, this.treeStartNormalRegion, this.treeStartSelectRegion, game.getVertexBufferObjectManager(), this, new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				game.switchGameToRunning(true, 0, 100, 0);
				if(Settings.soundEnabled) {
					game.mButtonSound.play();
				}
			}
		});
		this.registerTouchArea(this.treeStart);
		this.getChildByIndex(LAYER_FLOW).attachChild(this.treeStart);
		
		//Tree Body Left
		this.treeAfter[1] = new EventButtonSprite(284, 404, this.treeAfter02Region, this.treeAfter02SelectRegion, game.getVertexBufferObjectManager(), this, new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				game.switchGameToRunning(true, 8, 100, 0);
				if(Settings.soundEnabled) {
					game.mButtonSound.play();
				}
			}
		});
		this.registerTouchArea(this.treeAfter[1]);
		this.getChildByIndex(LAYER_FLOW).attachChild(this.treeAfter[1]);
		
		//Tree Body Right
		this.treeAfter[2] = new EventButtonSprite(284, 404, this.treeAfter03Region, this.treeAfter03SelectRegion, game.getVertexBufferObjectManager(), this, new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				game.switchGameToRunning(true, 8, 100, 0);
				if(Settings.soundEnabled) {
					game.mButtonSound.play();
				}
			}
		});
		this.registerTouchArea(this.treeAfter[2]);
		this.getChildByIndex(LAYER_FLOW).attachChild(this.treeAfter[2]);
		
		//Tree Body
		this.treeAfter[3] = new EventButtonSprite(284, 404, this.treeAfter04Region, this.treeAfter04SelectRegion, game.getVertexBufferObjectManager(), this, new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				game.switchGameToRunning(true, 8, 100, 0);
				if(Settings.soundEnabled) {
					game.mButtonSound.play();
				}
			}
		});
		this.registerTouchArea(this.treeAfter[3]);
		this.getChildByIndex(LAYER_FLOW).attachChild(this.treeAfter[3]);
		
		//Tree Half
		this.treeAfter[4] = new EventButtonSprite(284, 404, this.treeAfter05Region, this.treeAfter05SelectRegion, game.getVertexBufferObjectManager(), this, new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				game.switchGameToRunning(true, 8, 100, 0);
				if(Settings.soundEnabled) {
					game.mButtonSound.play();
				}
			}
		});
		this.registerTouchArea(this.treeAfter[4]);
		this.getChildByIndex(LAYER_FLOW).attachChild(this.treeAfter[4]);
		
		
		
		this.setOnSceneTouchListener(new IOnSceneTouchListener() {
			Vector2 tempP = new Vector2();
			
			@Override
			public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
				// TODO Auto-generated method stub
				
				Vector2 p1 = new Vector2();
				
				switch(pSceneTouchEvent.getAction()) {
				case TouchEvent.ACTION_DOWN:
					this.tempP.set(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
					break;
				case TouchEvent.ACTION_MOVE:
					
					p1.set(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
					
					EventEntity entity = (EventEntity)GameScene.this.getChildByIndex(LAYER_FLOW);
					
					if(!entity.isVisible())
						return false;
					
					float hTemp = entity.getY();
					if(entity.getY() >= -300 && entity.getY() <= 0) {
						hTemp = entity.getY() + p1.sub(tempP).y;
						if(hTemp > 0) {
							hTemp = 0;
						} else {
							if(hTemp < -300) {
								hTemp = -300;
							}
						}
						entity.setPosition(0, hTemp);
						
						this.tempP.set(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
					}
					break;
				case TouchEvent.ACTION_UP:
					break;
				
				}
				
				return true;
			}
		});
		
		//UI LAYER
		Sprite up = new Sprite(0, 0, this.upRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LAYER_UI).attachChild(up);
		Sprite down = new Sprite(0, 404, this.downRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LAYER_UI).attachChild(down);
		
		Sprite achieveBack = new Sprite(0, 8, this.achieveBackRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LAYER_UI).attachChild(achieveBack);
		this.achieveBar = new Sprite(117, 24, this.achieveBarRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LAYER_UI).attachChild(this.achieveBar);
		
		updateAchieveBar();
		
		Sprite bookmarkBack = new Sprite(497, 18, this.bookmarkBackRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LAYER_UI).attachChild(bookmarkBack);
		for(int i=0 ; i<bookmarkIcon.length ; i++) {
			bookmarkIcon[i] = new Sprite(497+i*38, 18, this.bookmarkIconRegion, game.getVertexBufferObjectManager());
			this.getChildByIndex(LAYER_UI).attachChild(this.bookmarkIcon[i]);
		}
		this.bookmarkEffect = new AnimatedSprite(497, 18, this.bookmarkEffectRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LAYER_UI).attachChild(this.bookmarkEffect);
		this.bookmarkEffect.setVisible(false);
		
		updateBookmarkIcon(false);
		
		//Social Layer!
		this.btnSocial = new EventButtonSprite(681, 363, this.btnSocialNormalRegion, this.btnSocialSelectRegion, game.getVertexBufferObjectManager(), this, new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				GameScene.this.getChildByIndex(LAYER_FLOW).setVisible(false);
				GameScene.this.getChildByIndex(LAYER_UI).setVisible(false);
				GameScene.this.getChildByIndex(LAYER_SOCIAL_1).setVisible(true);
				GameScene.this.getChildByIndex(LAYER_SOCIAL_2).setVisible(false);
				
				if(Settings.soundEnabled) {
					game.mButtonSound.play();
				}
			}
		});
		this.registerTouchArea(this.btnSocial);
		this.getChildByIndex(LAYER_UI).attachChild(this.btnSocial);
		
		this.mAchievePercentText = new Text(400, 19, this.mBitmapFont, Integer.toString((int)game.mAchieve) + "%", 500, game.getVertexBufferObjectManager());
		this.getChildByIndex(LAYER_UI).attachChild(this.mAchievePercentText);
		
		//Sprite p10 = new Sprite(425, 21, this.p_10Region, game.getVertexBufferObjectManager());
		//this.getChildByIndex(LAYER_UI).attachChild(p10);
		
		
		Sprite bookmarkBox = new Sprite(696 , 18, this.bookmarkBoxRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LAYER_UI).attachChild(bookmarkBox);
		
		bookmarkMax = new Sprite(696 , 18, this.bookmarkMaxRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LAYER_UI).attachChild(this.bookmarkMax);
	
		String time = TimeUtils.formatSeconds(game.mTime);
		this.mRemainTimeText = new Text(696+20, 18+10, this.mBitmapFont, time, game.getVertexBufferObjectManager());
		this.getChildByIndex(LAYER_UI).attachChild(mRemainTimeText);
		updateBookmarkMax();
		
		this.registerUpdateHandler(new TimerHandler(1.f, true, new ITimerCallback() {
			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				// TODO Auto-generated method stub
				GameScene.this.handleRemainTimer();
			}
		}));
		
		//Social Layer 1
		Sprite socialFSelect = new Sprite(15, 12, this.socialFSelectRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LAYER_SOCIAL_1).attachChild(socialFSelect);
		
		this.socialRNormalN = new EventButtonSprite(243 , 12, this.socialRNormalNRegion, this.socialRNormalSRegion, game.getVertexBufferObjectManager(), this, new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				
				GameScene.this.getChildByIndex(LAYER_FLOW).setVisible(false);
				GameScene.this.getChildByIndex(LAYER_UI).setVisible(false);
				GameScene.this.getChildByIndex(LAYER_SOCIAL_1).setVisible(false);
				GameScene.this.getChildByIndex(LAYER_SOCIAL_2).setVisible(true);
				
				if(Settings.soundEnabled) {
					game.mButtonSound.play();
				}
			}
		});
		this.registerTouchArea(this.socialRNormalN);
		this.getChildByIndex(LAYER_SOCIAL_1).attachChild(this.socialRNormalN);
		
		EventButtonSprite kakao01 = new EventButtonSprite(36, 375, this.btnKakao01NormalRegion, this.btnKakao01NormalRegion, game.getVertexBufferObjectManager(), this, new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				try {
					game.sendAppData(game.myInfo.getName() + "님이 " + game.rListFriend.get(0).getName() + "님을 \"사도\"캐릭터로 설정하였습니다.");
					
				} catch(Exception e) {
					
				}
			}
		});
		this.registerTouchArea(kakao01);
		this.getChildByIndex(LAYER_SOCIAL_1).attachChild(kakao01);
		
		EventButtonSprite kakao02 = new EventButtonSprite(115, 375, this.btnKakao02NormalRegion, this.btnKakao02NormalRegion, game.getVertexBufferObjectManager(), this, new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				try {
					game.sendAppData("지난번 병원에서 받은 검사 결과를 확인하러 가는 날인데, 혹시 나쁜 결과가 나올 것 같아서 혼자 왔어 그런데 병원까지 " + game.rListFriend.get(0).getName() + "가 따라 왔는데, 엘리베이터를 기다릴까? 먼저 올라갈까?");
				} catch(Exception e) {
					
				}
			}
		});
		this.registerTouchArea(kakao02);
		this.getChildByIndex(LAYER_SOCIAL_1).attachChild(kakao02);
		
		EventButtonSprite kakao03 = new EventButtonSprite(194, 375, this.btnKakao03NormalRegion, this.btnKakao03NormalRegion, game.getVertexBufferObjectManager(), this, new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				try {
					game.sendAppData("그래? 그럼 먼저 올라가.. 따라 올라갈게.");
				} catch(Exception e) {
					
				}
			}
		});
		this.registerTouchArea(kakao03);
		this.getChildByIndex(LAYER_SOCIAL_1).attachChild(kakao03);
		
		EventButtonSprite kakao04 = new EventButtonSprite(273, 375, this.btnKakao04NormalRegion, this.btnKakao04NormalRegion, game.getVertexBufferObjectManager(), this, new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				
				try {
					game.sendAppData("조금만 기다려. 바로 갈게");
				} catch(Exception e) {
					
				}
			}
		});
		this.registerTouchArea(kakao04);
		this.getChildByIndex(LAYER_SOCIAL_1).attachChild(kakao04);
		
		for(int i=0 ; i<game.mFriendNum ; i++) {
			Sprite friendchoiceFNormal = new Sprite(58+i*116, 103, this.friendchoiceFNormalRegion, game.getVertexBufferObjectManager());
			this.getChildByIndex(LAYER_SOCIAL_1).attachChild(friendchoiceFNormal);
			
			Sprite profileImage = new Sprite(17, 37, propicRegion.get(Integer.parseInt(game.listFriend.get(i).getNo())), game.getVertexBufferObjectManager());
			friendchoiceFNormal.attachChild(profileImage);
			
			String achieve = game.listFriend.get(i).getAchieve();
			achieve = achieve + "%";
			Text profileAchieve = new Text((this.friendchoiceFNormalRegion.getWidth()-achieve.length()*ACHIEVE_FONT_SIZE/2)/2, 11, this.mAchieveFont, achieve, new TextOptions(HorizontalAlign.CENTER), game.getVertexBufferObjectManager());
			friendchoiceFNormal.attachChild(profileAchieve);
			
			String name = game.listFriend.get(i).getName();
			Text profileName = new Text((this.friendchoiceFNormalRegion.getWidth()-name.length()*NAME_FONT_SIZE)/2, 112, this.mNameFont, name, game.getVertexBufferObjectManager());
			friendchoiceFNormal.attachChild(profileName);
		}
		
		this.btnBack1 = new EventButtonSprite(704 , 384, this.btnBackNormalRegion, this.btnBackSelectRegion, game.getVertexBufferObjectManager(), this, new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				GameScene.this.getChildByIndex(LAYER_FLOW).setVisible(true);
				GameScene.this.getChildByIndex(LAYER_UI).setVisible(true);
				GameScene.this.getChildByIndex(LAYER_SOCIAL_1).setVisible(false);
				GameScene.this.getChildByIndex(LAYER_SOCIAL_2).setVisible(false);
				
				if(Settings.soundEnabled) {
					game.mButtonSound.play();
				}
			}
		});
		this.registerTouchArea(this.btnBack1);
		this.getChildByIndex(LAYER_SOCIAL_1).attachChild(this.btnBack1);
		
		this.getChildByIndex(LAYER_SOCIAL_1).setVisible(false);
		
		//Social Layer 2
		this.socialFNormalN = new EventButtonSprite(15 , 12, this.socialFNormalNRegion, this.socialFNormalSRegion, game.getVertexBufferObjectManager(), this, new OnClickListener () {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				
				GameScene.this.getChildByIndex(LAYER_FLOW).setVisible(false);
				GameScene.this.getChildByIndex(LAYER_UI).setVisible(false);
				GameScene.this.getChildByIndex(LAYER_SOCIAL_1).setVisible(true);
				GameScene.this.getChildByIndex(LAYER_SOCIAL_2).setVisible(false);
				
				if(Settings.soundEnabled) {
					game.mButtonSound.play();
				}
			}
		});
		this.getChildByIndex(LAYER_SOCIAL_2).attachChild(socialFNormalN);
		

		Sprite socialRSelect = new Sprite(15, 12, this.socialRSelectRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LAYER_SOCIAL_2).attachChild(socialRSelect);
		
		//받은요청. 
		Sprite socialRFromF = new Sprite(14, 68, this.socialRFromFRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LAYER_SOCIAL_2).attachChild(socialRFromF);
		
		//보낸요청.
		Sprite socialRToF = new Sprite(14, 264, this.socialRToFRegion, game.getVertexBufferObjectManager());
		this.getChildByIndex(LAYER_SOCIAL_2).attachChild(socialRToF);
		
		for(int i=0 ; i<game.mRequestNum ; i++) {
			EventButtonSprite friendchoiceRNormal = new EventButtonSprite(58+i*116, 103, this.friendchoiceRNormalRegion, this.friendchoiceRSelectRegion, game.getVertexBufferObjectManager(), this, new OnClickListener() {
				@Override
				public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
					// TODO Auto-generated method stub	
					
					Debug.d(TAG, "button tag : " + pButtonSprite.getTag());
					
					game.switchGameToRunning(true, 9, pButtonSprite.getTag()-100, 0);
					
					GameScene.this.getChildByIndex(LAYER_FLOW).setVisible(true);
					GameScene.this.getChildByIndex(LAYER_UI).setVisible(true);
					GameScene.this.getChildByIndex(LAYER_SOCIAL_1).setVisible(false);
					GameScene.this.getChildByIndex(LAYER_SOCIAL_2).setVisible(false);
					
				}
			});
			friendchoiceRNormal.setTag(100 + i);
			this.getChildByIndex(LAYER_SOCIAL_2).attachChild(friendchoiceRNormal);
			
			Sprite profileImage = new Sprite(17, 24, propicRegion.get(Integer.parseInt(game.rListFriend.get(i).getNo())), game.getVertexBufferObjectManager());
			friendchoiceRNormal.attachChild(profileImage);
			
			
			String name = game.rListFriend.get(i).getName();
			Text profileName = new Text((this.friendchoiceFNormalRegion.getWidth()-name.length()*NAME_FONT_SIZE)/2, 112, this.mNameFont, name, game.getVertexBufferObjectManager());
			friendchoiceRNormal.attachChild(profileName);
		}
		
		for(int i=0 ; i<game.mReplyNum ; i++) {
			EventButtonSprite friendchoiceRNormal = new EventButtonSprite(58+i*116, 299, this.friendchoiceRNormalRegion, this.friendchoiceRSelectRegion, game.getVertexBufferObjectManager(), this, new OnClickListener() {
				@Override
				public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
					// TODO Auto-generated method stub
					
					String temp = game.myInfo.getReplyId();
					Debug.d(TAG, "reply : " + temp);
					String[] temp2 = temp.split(",");
					int num = Integer.parseInt(temp2[1]);
					
					Debug.d(TAG, "num =======================> " + num);
					
					game.switchGameToRunning(true, 8, pButtonSprite.getTag()-200, num);
					
					//game.replyListFriend.remove(pButtonSprite.getTag()-200);
					
					GameScene.this.getChildByIndex(LAYER_FLOW).setVisible(true);
					GameScene.this.getChildByIndex(LAYER_UI).setVisible(true);
					GameScene.this.getChildByIndex(LAYER_SOCIAL_1).setVisible(false);
					GameScene.this.getChildByIndex(LAYER_SOCIAL_2).setVisible(false);
				}
			});
			friendchoiceRNormal.setTag(200 + i);
			this.getChildByIndex(LAYER_SOCIAL_2).attachChild(friendchoiceRNormal);
			
			Sprite profileImage = new Sprite(17, 24, propicRegion.get(Integer.parseInt(game.replyListFriend.get(i).getNo())), game.getVertexBufferObjectManager());
			friendchoiceRNormal.attachChild(profileImage);
			
			
			String name = game.replyListFriend.get(i).getName();
			Text profileName = new Text((this.friendchoiceFNormalRegion.getWidth()-name.length()*NAME_FONT_SIZE)/2, 112, this.mNameFont, name, game.getVertexBufferObjectManager());
			friendchoiceRNormal.attachChild(profileName);
		}
		
		this.btnBack2 = new EventButtonSprite(704, 384, this.btnBackNormalRegion, this.btnBackSelectRegion, game.getVertexBufferObjectManager(), this, new OnClickListener() {
			@Override
			public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) {
				// TODO Auto-generated method stub
				GameScene.this.getChildByIndex(LAYER_FLOW).setVisible(true);
				GameScene.this.getChildByIndex(LAYER_UI).setVisible(true);
				GameScene.this.getChildByIndex(LAYER_SOCIAL_1).setVisible(false);
				GameScene.this.getChildByIndex(LAYER_SOCIAL_2).setVisible(false);
				
				if(Settings.soundEnabled) {
					game.mButtonSound.play();
				}
			}
		});
		
		this.getChildByIndex(LAYER_SOCIAL_2).attachChild(btnBack2);
		
		this.getChildByIndex(LAYER_SOCIAL_2).setVisible(false);
		
		
		sequenceModifier = new SequenceEntityModifier(
				new IEntityModifierListener() {

					@Override
					public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
						// TODO Auto-generated method stub
					}

					@Override
					public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
						// TODO Auto-generated method stub
					}
				},
				
				new DelayModifier(0.5f),
				new MoveModifier(0.5f, 0, 0, GameScene.this.getChildByIndex(LAYER_FLOW).getY(), -240)
		);
		
		updateTreeAfter();
	}
	
	@Override
	public void updateScene(int sceneNumber, int friendNumber, int num) {
		// TODO Auto-generated method stub
		this.updateAchieveBar();
		this.updateBookmarkIcon(false);
		this.updateBookmarkMax();
		this.updateTreeAfter();
		if(sceneNumber == 1) {
			this.getChildByIndex(LAYER_FLOW).registerEntityModifier(sequenceModifier);
		}
	}
	
	

	private void handleRemainTimer() {
		if(!this.bookmarkMax.isVisible()) {
			if(game.mTime > 0) {
				game.mTime --;
				this.mRemainTimeText.setText(TimeUtils.formatSeconds(game.mTime));
			} else {
				game.mTime = 300;
				if(game.mBookmark < 5) {
					++ game.mBookmark;
					this.updateBookmarkIcon(true);
					this.updateBookmarkMax();
				}
				
			}
		}
	}
	
	public void updateReplyFriend() {
		
	}
	
	public void updateBookmarkMax() {
		if(game.mBookmark > 4) {
			this.bookmarkMax.setVisible(true);
			this.mRemainTimeText.setVisible(false);
		} else {
			this.bookmarkMax.setVisible(false);
			this.mRemainTimeText.setVisible(true);
		}
	}
	
	public void updateBookmarkIcon(boolean bEffect) {
		for(int i=0 ; i<bookmarkIcon.length ; i++) 
			bookmarkIcon[i].setVisible(false);
		
		for(int i=0 ; i<game.mBookmark ; i++)
			bookmarkIcon[i].setVisible(true);
		
		if(bEffect) {
			bookmarkEffect.setPosition(bookmarkIcon[game.mBookmark-1]);
			bookmarkEffect.setVisible(true);
			bookmarkEffect.animate(new long[] {200, 200}, false, new IAnimationListener() {
				@Override
				public void onAnimationFinished(AnimatedSprite pAnimatedSprite) {
					// TODO Auto-generated method stub
					GameScene.this.bookmarkEffect.stopAnimation();
					GameScene.this.bookmarkEffect.setVisible(false);
				}

				@Override
				public void onAnimationStarted(AnimatedSprite pAnimatedSprite,
						int pInitialLoopCount) {
					// TODO Auto-generated method stub
				}

				@Override
				public void onAnimationFrameChanged(AnimatedSprite pAnimatedSprite, int pOldFrameIndex, int pNewFrameIndex) {
					// TODO Auto-generated method stub
				}

				@Override
				public void onAnimationLoopFinished(AnimatedSprite pAnimatedSprite, int pRemainingLoopCount, int pInitialLoopCount) {
					// TODO Auto-generated method stub
				}
			});
		}
	}
	
	public void updateAchieveBar() {
		this.achieveBar.setScaleCenter(0.f, 0.f);
		float value = game.mAchieve / 100.f;
		this.achieveBar.setScaleX(value);
		Debug.d(TAG, "mAchieve : " + game.mAchieve);
		if(this.mAchievePercentText != null) {
			
			String achievePer = Integer.toString((int)game.mAchieve) + "%";
			this.mAchievePercentText.setText(achievePer);
		}
	}

	public void updateTreeAfter() {
		this.treeBeforeSelect01.setVisible(false);
		this.treeBeforeSelect02.setVisible(false);
		for(int i=0 ; i<treeAfter.length ; i++)
			treeAfter[i].setVisible(false);
		
		for(int i=0 ; i<game.isPass.length ; i++) {
			Debug.d(TAG, "isPass : " + game.isPass[i]);
		}
		
		if(game.isPass[3] && game.isPass[2]) {
			treeAfter[0].setVisible(true);
			treeAfter[3].setVisible(true);
			game.mAchieve = 100;
		} else if(game.isPass[3]) {
			treeAfter[0].setVisible(true);
			treeAfter[2].setVisible(true);
			game.mAchieve = 88;
		} else if(game.isPass[2]) {
			treeAfter[0].setVisible(true);
			treeAfter[1].setVisible(true);
			game.mAchieve = 88;
		} else if(game.isPass[1]) {
			treeAfter[0].setVisible(true);
			treeAfter[4].setVisible(true);
			game.mAchieve = 64;
		} else if(game.isPass[0]) {
			treeAfter[0].setVisible(true);
			this.treeBeforeSelect02.setVisible(true);
			game.mAchieve = 48;
		} else {
			this.treeBeforeSelect01.setVisible(true);
			game.mAchieve = 0;
		}
		
		updateAchieveBar();
	}
	
	SequenceEntityModifier sequenceModifier;
	
}
